//
//  ShengMingCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/8/20.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "ShengMingCon.h"
#import <WebKit/WebKit.h>

@interface ShengMingCon ()<WKNavigationDelegate>

@property (strong, nonatomic) WKWebView *webview;
@property(nonatomic,strong)UIView *bottomview;



@end

@implementation ShengMingCon

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bottomview = [UIView new];
    [self.view addSubview:self.bottomview];
    self.bottomview.sd_layout
    .bottomSpaceToView(self.view, 0)
    .leftSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .heightIs(50*kJLXWidthScale);
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"关闭" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(closeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomview addSubview:btn];
    btn.sd_layout
    .centerYEqualToView(self.bottomview)
    .centerXEqualToView(self.bottomview)
    .heightIs(50*kJLXWidthScale)
    .widthIs(100*kJLXWidthScale);
 
    self.webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    self.webview.navigationDelegate = self;
    [self.view addSubview:self.webview];
    self.webview.sd_layout
    .topSpaceToView(self.view, 0)
    .leftSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .bottomSpaceToView(self.bottomview, 0);
//    self.webview.delegate = self;
    [self loadWebViewString:self.contentstr];
    
}

-(void)closeButtonAction:(UIButton *)sender{
    self.view.superview.hidden = YES;
    [self.view.superview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

-(void)loadWebViewString:(NSString *)loadString
{
    NSString *content = [[NSString alloc] init];
    if (loadString == nil || loadString == NULL) {
        content =@"";
    }else
        content = [NSString stringWithFormat:@"<html> \n"
                   "<head> \n"
                   "<style type=\"text/css\"> \n"
                   "body {font-size:15px;}\n"
                   "</style> \n"
                   "</head> \n"
                   "<body>"
                   "<script type='text/javascript'>"
                   "window.onload = function(){\n"
                   "var $img = document.getElementsByTagName('img');\n"
                   "for(var p in  $img){\n"
                   " $img[p].style.width = '100%%';\n"
                   "$img[p].style.height ='auto'\n"
                   "}\n"
                   "}"
                   "</script>%@"
                   "</body>"
                   "</html>",loadString];
    [_webview loadHTMLString:content baseURL:nil];
}

#pragma =======webviewDeleget==========


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"加载完成");
    
    [webView evaluateJavaScript:@"document.body.scrollWidth"completionHandler:^(id _Nullable result,NSError * _Nullable error){

        double width = [result floatValue];
        [webView evaluateJavaScript:@"document.body.scrollHeight"completionHandler:^(id _Nullable result,NSError * _Nullable error){

            double height = [result floatValue] / width * kScreenWidth;
            NSLog(@"scrollHeight高度：%.2f",height);
            self.webview.frame = CGRectMake(self.webview.frame.origin.x,self.webview.frame.origin.y, kScreenWidth, height);
       
        }];

//        self.webview.frame = CGRectMake(self.webview.frame.origin.x,self.webview.frame.origin.y, kScreenWidth, [result floatValue] + 30*kJLXWidthScale);
//        self.webHeight = [result floatValue];
//        [self.listTableview reloadData];
   
    }];
    
}
@end

